{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: azure-identity==1.15.0 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (1.15.0)\n",
      "Requirement already satisfied: azure-ai-contentsafety==1.0.0 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (1.0.0)\n",
      "Collecting pandas==2.2.0\n",
      "  Using cached pandas-2.2.0-cp311-cp311-macosx_11_0_arm64.whl.metadata (19 kB)\n",
      "Requirement already satisfied: azure-core<2.0.0,>=1.23.0 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from azure-identity==1.15.0) (1.29.6)\n",
      "Requirement already satisfied: cryptography>=2.5 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from azure-identity==1.15.0) (41.0.1)\n",
      "Requirement already satisfied: msal<2.0.0,>=1.24.0 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from azure-identity==1.15.0) (1.25.0)\n",
      "Requirement already satisfied: msal-extensions<2.0.0,>=0.3.0 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from azure-identity==1.15.0) (1.0.0)\n",
      "Requirement already satisfied: isodate<1.0.0,>=0.6.1 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from azure-ai-contentsafety==1.0.0) (0.6.1)\n",
      "Requirement already satisfied: numpy<2,>=1.23.2 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from pandas==2.2.0) (1.26.1)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from pandas==2.2.0) (2.8.2)\n",
      "Requirement already satisfied: pytz>=2020.1 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from pandas==2.2.0) (2023.3)\n",
      "Requirement already satisfied: tzdata>=2022.7 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from pandas==2.2.0) (2023.3)\n",
      "Requirement already satisfied: anyio<5.0,>=3.0 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from azure-core<2.0.0,>=1.23.0->azure-identity==1.15.0) (3.7.1)\n",
      "Requirement already satisfied: requests>=2.21.0 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from azure-core<2.0.0,>=1.23.0->azure-identity==1.15.0) (2.31.0)\n",
      "Requirement already satisfied: six>=1.11.0 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from azure-core<2.0.0,>=1.23.0->azure-identity==1.15.0) (1.16.0)\n",
      "Requirement already satisfied: typing-extensions>=4.6.0 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from azure-core<2.0.0,>=1.23.0->azure-identity==1.15.0) (4.8.0)\n",
      "Requirement already satisfied: cffi>=1.12 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from cryptography>=2.5->azure-identity==1.15.0) (1.15.1)\n",
      "Requirement already satisfied: PyJWT<3,>=1.0.0 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from PyJWT[crypto]<3,>=1.0.0->msal<2.0.0,>=1.24.0->azure-identity==1.15.0) (2.8.0)\n",
      "Requirement already satisfied: portalocker<3,>=1.0 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from msal-extensions<2.0.0,>=0.3.0->azure-identity==1.15.0) (2.7.0)\n",
      "Requirement already satisfied: idna>=2.8 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from anyio<5.0,>=3.0->azure-core<2.0.0,>=1.23.0->azure-identity==1.15.0) (3.4)\n",
      "Requirement already satisfied: sniffio>=1.1 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from anyio<5.0,>=3.0->azure-core<2.0.0,>=1.23.0->azure-identity==1.15.0) (1.3.0)\n",
      "Requirement already satisfied: pycparser in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from cffi>=1.12->cryptography>=2.5->azure-identity==1.15.0) (2.21)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from requests>=2.21.0->azure-core<2.0.0,>=1.23.0->azure-identity==1.15.0) (3.2.0)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from requests>=2.21.0->azure-core<2.0.0,>=1.23.0->azure-identity==1.15.0) (1.26.16)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from requests>=2.21.0->azure-core<2.0.0,>=1.23.0->azure-identity==1.15.0) (2023.5.7)\n",
      "Using cached pandas-2.2.0-cp311-cp311-macosx_11_0_arm64.whl (11.8 MB)\n",
      "Installing collected packages: pandas\n",
      "  Attempting uninstall: pandas\n",
      "    Found existing installation: pandas 2.0.3\n",
      "    Uninstalling pandas-2.0.3:\n",
      "      Successfully uninstalled pandas-2.0.3\n",
      "Successfully installed pandas-2.2.0\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install azure-identity==1.15.0 azure-ai-contentsafety==1.0.0 pandas==2.2.0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Initialize clients and access keys."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from azure.ai.contentsafety import BlocklistClient\n",
    "from azure.ai.contentsafety.models import (\n",
    "    AddOrUpdateTextBlocklistItemsOptions,\n",
    "    TextBlocklist,\n",
    "    TextBlocklistItem,\n",
    ")\n",
    "from azure.core.credentials import AzureKeyCredential\n",
    "\n",
    "key = AzureKeyCredential(\"xxx\")\n",
    "client = BlocklistClient(\n",
    "    \"https://ccai-clesne-contentsafety.cognitiveservices.azure.com/\", key\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "List current blocklists."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "competitors: Competitors blocklist.\n"
     ]
    }
   ],
   "source": [
    "blocklists = client.list_text_blocklists()\n",
    "\n",
    "if not blocklists:\n",
    "    print(\"There are no blocklists.\")  # noqa: T201\n",
    "\n",
    "for blocklist in blocklists:\n",
    "    print(f\"{blocklist.blocklist_name}: {blocklist.description}\")  # noqa: T201"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Creating blocklist competitors with 53 words\n"
     ]
    }
   ],
   "source": [
    "name = \"competitors\"\n",
    "description = \"Competitors blocklist.\"\n",
    "\n",
    "df = pd.read_csv(\"blocklist.csv\")\n",
    "\n",
    "block_items = {}\n",
    "for index, row in df.iterrows():\n",
    "    text = TextBlocklistItem(text=row[\"word\"])\n",
    "    if row[\"blocklist\"] not in block_items:\n",
    "        block_items[row[\"blocklist\"]] = []\n",
    "    block_items[row[\"blocklist\"]].append(text)\n",
    "\n",
    "for blocklist, words in block_items.items():\n",
    "    print(f\"Creating blocklist {blocklist} with {len(words)} words\")  # noqa: T201\n",
    "    client.create_or_update_text_blocklist(\n",
    "        blocklist_name=blocklist,\n",
    "        options=TextBlocklist(blocklist_name=blocklist),\n",
    "    )\n",
    "    client.add_or_update_blocklist_items(\n",
    "        blocklist_name=blocklist,\n",
    "        options=AddOrUpdateTextBlocklistItemsOptions(blocklist_items=words),\n",
    "    )"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
